package com.jinsi.service.ylt.iml;


import com.jinsi.mapper.ylt.UserListMapper;
import com.jinsi.pojo.SyCourseLog;
import com.jinsi.pojo.SyUser;
import com.jinsi.pojo.SyUserLeading;
import com.jinsi.pojo.SyUserSeal;
import com.jinsi.service.ylt.CouponService;
import com.jinsi.service.ylt.UserListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName UserListService
 * @Content 用户列表service接口实现类
 * @Author 姚龙腾
 * @Date 2022/10/12 11:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserListServiceImpl implements UserListService {
    @Autowired
    private UserListMapper userListMapper;
    @Autowired
    private CouponService couponService;


    /**
     * 获取所有用户信息展示于列表
     * @param insider
     * @return List<SyCourseLog>
     */
    @Override
    @Transactional(readOnly = true)
    public List<SyCourseLog> getAll(Integer insider) {
        return userListMapper.getAll(insider);
    }

    /**
     * 更改用户账号状态
     * @param userId
     * @param userStats
     * @throws Exception
     */
    @Override
    public void upUserStats(Integer userId, Integer userStats) throws Exception {
        int num = userListMapper.upUserStats(userId, userStats);
        if (num == 0){
            throw new Exception();
        }
    }


    /**
     * 修改用户信息
     * @param user
     * @throws Exception
     */
    @Override
    public void upUser(SyUserLeading user) throws Exception {
        int num = userListMapper.upUser(user);
        if (num == 0){
            throw new Exception();
        }
    }


    /**
     * 根据输入信息进行模糊查询
     * @param insider
     * @param userName
     * @param userAccount
     * @param userCreateDate
     * @return List<SyCourseLog>
     */
    @Override
    @Transactional(readOnly = true)
    public List<SyCourseLog> getAllByLike(
            Integer insider,
            String userName,
            String userAccount,
            String userCreateDate) {
        return userListMapper.getAllByLike(
                insider,
                userName,
                userAccount,
                userCreateDate);
    }

    /**
     * 添加用户为进思用户
     * @param userIdArray
     */
    @Override
    public void upUserInsider(String[] userIdArray) {
        for (int i = 0; i < userIdArray.length; i++){
            int num =  Integer.valueOf(userIdArray[i]);
            userListMapper.upUserInsider(num);
        }
    }


    /**
     * 为用户发放优惠券
     * @param userArr
     * @param couponArr
     */
    @Override
    public void addSyDiscountsIssuance(String[] userArr, String[] couponArr){
        for (int i = 0; i < userArr.length; i++){
            for (int j = 0; j < couponArr.length; j++){
                Integer userId = Integer.valueOf(userArr[i]);
                Integer discountsId = Integer.valueOf(couponArr[j]);
                couponService.addUserCoupon(userId,discountsId);
                couponService.upCouponNum(discountsId,1);
            }
        }
    }


    /**
     * 封停用户账号
     * @param syUserSeal
     * @throws Exception
     */
    @Override
    public void addUserSeal(SyUserSeal syUserSeal) throws Exception {
        int num = userListMapper.addUserSeal(syUserSeal);
        if (num == 0){
            throw new Exception();
        }
    }


    /**
     * 在原有封停信息上再次添加封停
     * @param syUserSeal
     * @throws Exception
     *
     */
    @Override
    public void upUserSealDate(SyUserSeal syUserSeal) throws Exception {
        int num = userListMapper.upUserSealDate(syUserSeal);
        if (num == 0){
            throw new Exception();
        }
    }


}
